package com.uzhie.web.controller.http.comm;

import com.uzhie.util.PayloadBeanUtil;
import com.uzhie.web.bean.ErrorBean;
import com.uzhie.web.bean.PageBean;
import com.uzhie.web.bean.PayloadBean;
import com.uzhie.web.bean.ResponseBodyBean;
import org.apache.log4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * Created by lilongfei on 2018/7/16.
 */
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public abstract class CommController {

    protected Logger logger = Logger.getLogger(getClass());
    private PayloadBeanUtil payloadBeanUtil = PayloadBeanUtil.getInstance();

    protected ErrorBean setError(int status, String message) {
        ErrorBean errorBean = new ErrorBean();
        errorBean.setMsg(message);
        errorBean.setCode(status);
        return errorBean;
    }

    /**
     * 创建responseBody
     *
     * @param result
     * @param <T>
     * @return
     */
    protected <T> PayloadBean buildResponse(T result) {
        return buildResponse(result, setError(0, null));
    }

    /**
     * 创建responseBody
     *
     * @param result    结果
     * @param errorBean 异常信息
     * @param <T>
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    protected <T> PayloadBean buildResponse(T result, ErrorBean errorBean) {
        PayloadBean payloadBean = null;
        try {
            payloadBean = payloadBeanUtil.buildPayload(ResponseBodyBean.class, result, errorBean);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return payloadBean;
    }

    protected <T> PayloadBean buildResponse(T result, String msg) {
        PayloadBean payloadBean = null;
        try {
            payloadBean = payloadBeanUtil.buildPayload(ResponseBodyBean.class, result, setError(500, msg));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return payloadBean;
    }

    /**
     * 创建responseBody带分页
     *
     * @param result    结果
     * @param pageBean  分页
     * @param errorBean 异常信息
     * @param <T>
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    protected <T> PayloadBean<T> buildResponseWithPage(List<T> result, PageBean pageBean, ErrorBean errorBean) {
        PayloadBean payloadBean = null;
        try {
            payloadBean = payloadBeanUtil.buildPayload(ResponseBodyBean.class, result, pageBean, errorBean);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return payloadBean;
    }

    protected <T> PayloadBean<T> buildResponseWithPage(List<T> result, PageBean pageBean, String msg) {
        PayloadBean payloadBean = null;
        try {
            payloadBean = payloadBeanUtil.buildPayload(ResponseBodyBean.class, result, pageBean, setError(500, msg));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return payloadBean;
    }

    /**
     * 创建responseBody带分页
     *
     * @param result   结果集
     * @param pageBean 分页信息
     * @param <T>
     * @return
     */
    protected <T> PayloadBean<T> buildResponseWithPage(List<T> result, PageBean pageBean) {
        return buildResponseWithPage(result, pageBean, setError(0, "OK"));
    }


}
